import React from 'react';
import { connect } from 'react-redux';
import { fetchApproveArticle } from '../actions/article';

import Approval from '../components/approval';
import Message from 'antd/lib/message';
import axios from 'axios';
import { API_ROOT } from '../config.jsx';

class ReviewPage extends React.Component{

    constructor(props) {
        super(props);
        this.state={
            done: false
        }
    }

    componentDidMount(){
        sessionStorage.openId = this.props.params.openId;
        this.props.fetchApproveArticle(this.props.params.articleId);
    }

    reject(comment) {
        let _this = this;
        axios.defaults.headers.common['Authorization'] = sessionStorage.token;
        axios.post(`${API_ROOT}/wechatapi/Reject/${this.props.params.articleId}`, {key:comment})
        .then(function(ret) {
            Message.success('操作成功');
            _this.setState({done:true});
        }).catch(function(err) {

        });
    }

    resolve(comment) {
        let _this = this;
        axios.defaults.headers.common['Authorization'] = sessionStorage.token;
        axios.post(`${API_ROOT}/wechatapi/Approve/${this.props.params.articleId}`, {key:comment})
        .then(function(ret) {
            Message.success('操作成功');
            _this.setState({done:true});
        }).catch(function(err) {

        });
    }

    render(){
        
        if(this.props.article == null){
            return(<div>Loading</div>)
        }else{
            return(
                <Approval 
                    article={this.props.article} 
                    reject={this.reject.bind(this)}
                    resolve={this.resolve.bind(this)}
                    isGuest={sessionStorage.openId}
                    done={this.state.done}
                />
            );
        }
    }
}

function mapStateToProps(state){
    return { 
        article: state.article.article
    };
}

export default connect(mapStateToProps, { fetchApproveArticle })(ReviewPage);